﻿#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

#include<algorithm>
using namespace std;

////向下调整建堆
//void AdjustDown(vector<int>& arr, int parent,int n)
//{
//	int child = 2 * parent + 1;
//	while (child < n)
//	{
//		if (child + 1 < n && arr[child + 1] > arr[child])
//		{
//			child++;
//		}
//
//		if (arr[child] > arr[parent])
//		{
//			swap(arr[parent], arr[child]);
//			parent = child;
//			child = 2 * parent + 1;
//		}
//		else
//		{
//			break;
//		}
//	}
//}
//
//int test_main()
//{
//	vector<int> arr = { 12,654,34,5,-3,45,0,33,10 };
//
//	int n = arr.size();
//	for (auto e : arr)
//	{
//		cout << e << " ";
//	}
//	cout << endl;
//
//	//向下调整建堆
//	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
//	{
//		AdjustDown(arr, i, n);
//	}
//
//	int end = n - 1;//最后一个元素的下标
//	while (end > 0)
//	{
//		swap(arr[0],arr[end]);
//		AdjustDown(arr, 0, end);
//		end--;
//	}
//
//	for (auto e : arr)
//	{
//		cout << e << " ";
//	}
//	cout << endl;
//	return 0;
//}

#include"vector.h"

namespace vct
{
	void test1()
	{
		vector<string> vc;
		vc.push_back("111");
		vc.push_back("222");
		vc.push_back("333");
		vc.push_back("444");
		vc.push_back("444");
		vc.push_back("444");
		vc.push_back("444");
		vc.push_back("444");
		vc.push_back("555");

		//for (size_t i = 0; i < vc.size(); i++)
		//{
		//	cout << vc[i] << " ";
		//}
		//cout << endl;
		//cout << "size:" << vc.size() << endl;
		//cout << "capacity:" << vc.capacity() << endl;
		vector<string>::iterator it = vc.begin();
		while (it != vc.end())
		{
			cout << *it <<endl;
			++it;
		}
	}

	void test2()
	{
		vector<int> vc;
		vc.push_back(1);
		vc.push_back(2);
		vc.push_back(3);
		vc.push_back(4);
		vc.push_back(5);
		vector<int>::iterator it1 = vc.begin();
		while (it1 != vc.end())
		{
			*it1 += 10;
			cout << *it1 <<" ";
			++it1;
		}
		cout << endl;
		vector<int> vc2;
		vc2.push_back(1);
		vc2.push_back(2);
		vc2.push_back(3);
		vc2.push_back(4);
		vector<int>::const_iterator it2 = vc2.cbegin();

		while (it2 != vc2.cend())
		{
			//*it2 += 10;
			cout << *it2 <<" ";
			++it2;
		}
		cout << endl;
	}

	void test3()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
		v1.insert(v1.begin()+2, 10);
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test4()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
		//find函数，在算法库中有实现[first,last)
		//若找不到指定的值，返回last
		vector<int>::iterator pos = find(v1.begin(), v1.end(), 2);
		if (pos != v1.end())
		{
			pos = v1.insert(pos, 1000);//需要接收
			cout <<* pos << endl;
		}
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
		v1.erase(pos);
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test5()
	{
		vector<int> v1;
		v1.push_back(109);

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
		vector<int> v2;
		v2.push_back(222);

		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;
		v1.swap(v2);

		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test6()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<int>::iterator pos = find(v1.begin(), v1.end(), 4);
		if (pos != v1.end())
		{
			pos = v1.erase(pos);
			//如果删除的是最后一个，需要做检查，以免越界访问
			if (pos != v1.end())
				cout << *pos << endl;
		}
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test7()
	{
		std::vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);
		v1.push_back(6);
		v1.push_back(8);
		v1.push_back(10);
		std::vector<int>::iterator it = v1.begin();
		while (it != v1.end())
		{
			if (*it % 2 == 0)
			{
				it = v1.erase(it);
			}
			else
			{
				it++;
			}
		}

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test8()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		for (auto e : v1) {
			cout << e << " ";
		}

		vector<int> v2(v1);//拷贝构造
		cout <<  endl;
		for (auto e : v2){
			cout << e << " ";
		}

		vector<int> v3;
		vector<int> v4;
		v4 = v3 = v1;   //赋值

		cout <<  endl;
		for (auto e : v3){
			cout << e << " ";
		}

		cout <<  endl;
		for (auto e : v4){
			cout << e << " ";
		}

	}

	void test9()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		//string s("ABCDEF");

		//vector<int> v2(s.begin(), s.end());
		v1.resize(10, 6);
		for (auto e : v1) {
			cout << e << " ";
		}
		cout << endl;
	}

	void test10()
	{	
		//为了适配模板
		//c++对内置类型进行了升级，也有默认构造
		int i = 10;
		int j(11);
		int k = int();
		int m = int(2);
	}
}

int main()
{
	vct::test9();
	return 0;
}


//int main()
//{
//	int ar[] = { 1,2,3,4,0,5,6,7,8,9 };
//	int n = sizeof(ar) / sizeof(int);
//	vector<int> v(ar, ar + n);
//	vector<int>::iterator it = v.begin();
//	while (it != v.end())
//	{
//		if (*it != 0)
//			cout << *it;
//		else
//			v.erase(it);
//		it++;
//	}
//	return 0;
//
//}

